using System;
using System.Collections.Generic;
using System.Linq;
using Gj.Galaxy.Logic;

namespace Gj.MapData.Strategy
{
    public class DynamicList
    {
        internal List<ExtendItem> _list = new List<ExtendItem>();

        public DynamicList(ExtendItem[] items)
        {
            _list.AddRange(items);
        }
        public DynamicList()
        {
        }
        public object this[int index]
        {
            get
            {
                if (index >= _list.Count) return 0;
                // Debug.LogErrorFormat("list: {0}->{1}", _list.Length, index);
                return _list[index].Value;
            }
            set
            {
                if (index >= _list.Count)
                {
                    var t = new ExtendItem[index-_list.Count+1];
                    _list.InsertRange(_list.Count, t);
                }

                if (_list[index] == null) _list[index] = new ExtendItem(index, null);
                _list[index].Value = value;
            }
        }

        public void Append(ExtendItem item)
        {
            _list.Add(item);
        }

        public void Delete(int no)
        {
            var index = this._list.FindIndex((row) => row.No == no);
            if (index < 0 || index >= _list.Count) return;
            _list.RemoveAt(index);
        }

        public void Reset(ExtendItem[] list)
        {
            for (var i = 0; i < list.Length; i++) this[i] = list[i];
        }

        public ExtendItem Get(int no)
        {
            var index = _list.FindIndex((row) => row.No == no);
            if (index == -1) return null;
            return _list[index];
        }

        public object GetValue(int no)
        {
            var index = _list.FindIndex((row) => row.No == no);
            if (index == -1) return 0;
            return _list[index].Value;
        }

        public int Total()
        {
            int num = 0;
            for (var i = 0; i < _list.Count; i++) if (_list[i] != null) num += (int)_list[i].Value;
            return num;
        }

        public bool Bool(int index)
        {
            var a = this[index];
            if (a == null) return false;
            if (a is bool) return (bool)a;
            if (a is int) return (int)a > 0;
            if (a is byte) return (byte)a > 0;
            if ((string)a == "1")
                return true;
            return false;
        }

        public int Int(int index)
        {
            var a = this[index];
            if (a == null) return 0;
            if (a is int) return (int)a;
            if (a is byte) return (byte)a;
            if (a is long) return Convert.ToInt32(a);
            var s = (string)a;
            if (s.Length == 0) return 0;
            return int.Parse(s);
        }

        public string String(int index)
        {
            var a = this[index];
            if (a == null) return "";
            if (a is string) return (string)a;
            return Convert.ToString(a);
        }
    }
}